<?php
App::uses('AppController', 'Controller');
class PackagesController extends AppController {	
	public $uses = array('Services','Category','Branches','Packages','PackagesItems','Clients','PackagesClients');
	public $components = array('Paginator');

	public function index(){
		$pagination_settings= array();
		if(isset($this->data['keyword'])){
			$keyword = $this->data['keyword'];
			$pagination_settings['conditions']= array('Packages.code LIKE' => "$keyword%");			
		}
		$pagination_settings['limit'] =30;
		$this->Paginator->settings =$pagination_settings;
    $packages = $this->Paginator->paginate('Packages');
		$this->set('data',$packages);	
	}

	public function add(){
		if(!empty($this->data))
		{		 
			$this->Packages->set($this->data);
			if($this->Packages->validates()){
				

			if($this->Packages->save()){

				$package_id=  $this->Packages->getLastInsertId();
				$this->redirect('/packages/add_services/'.$package_id);
				$this->Session->setFlash(__("Error", true), 'flash', array('class' => 'alert alert-error'));	
			}
			}
			else{
				$errors = $this->Packages->validationErrors;
				$this->Session->setFlash(__("Verifique los valores ingresados, algun dato es incorrecto", true), 'flash', array('class' => 'alert alert-error'));
			}	
		}

	}

	public function edit($id){
		if(!empty($this->data))
		{
			$this->Packages->id = $id;  
			$this->Packages->set($this->data);			
			if (!empty($this->data)){
	    	$this->Packages->id = $id;
	      if ($this->Packages->save($this->data)) {
	        $this->redirect('/packages');
	      }
	    }
			else{
				$errors = $this->Packages->validationErrors;
				$this->Session->setFlash(__("Verifique los valores ingresados, algun dato es incorrecto", true), 'flash', array('class' => 'alert alert-error'));
			}	
		}
		else{
			$row= $this->Packages->findById($id);			
			$new_date = ($row['Packages']['expiration_date']!='')? date("d-m-Y", strtotime($row['Packages']['expiration_date'])) : '';
			$row['Packages']['expiration_date'] = $new_date;
			$this->request->data =$row;
		}
		//$this->render("add");
	}

	public function add_services($id){
		

		$services = $this->Services->find('list');
		$services_codes = $this->Services->find('list',array('fields' => array('Services.id', 'Services.service_code')));	
		$package = $this->Packages->findById($id);
		//var_dump($package);
		$this->set('package',$package);	
		$this->set('services',$services);	
		$this->set('services_codes',$services_codes);		
	} 

	public function add_service_ajax(){
		$this->layout=false;
		$this->render(false);
		$this->request->data['PackagesItems']=$this->data;
		$data['PackagesItems']=array(
			'packages_id' => $this->data['packages_id'],
			'service_id' => $this->data['service_id'],
			'sessions' => $this->data['sessions'],
			);
		$this->request->data = $data;
		if(!empty($this->request->data))
		{			
			$this->PackagesItems->set($this->request->data);
			if($this->PackagesItems->validates())
			{
				if($this->PackagesItems->save())
				{					
					$this->add_total($data['PackagesItems']['packages_id']);
					$packages= $this->PackagesItems->find('all',array('conditions'=>array('PackagesItems.packages_id'=>$data['PackagesItems']['packages_id'])));
					echo json_encode($packages);
				}
			}
			else{
				$errors = $this->PackagesItems->validationErrors;
				echo json_encode($errors);
			}
		}		
	}

	public function delete_service_ajax(){
		$this->layout=false;
		$this->render(false);
		$result= $this->PackagesItems->delete($this->request->data('id'));		
		$this->delete_total($this->request->data('id'));
		$packages= $this->PackagesItems->find('all',array('conditions'=>array('PackagesItems.packages_id'=>$this->request->data('packages_id'))));
		echo json_encode($packages);
	}

	public function add_total($id){
		$package = $this->Packages->read(null,$id); 
		
			$package['Packages']['total_services'] = $package['Packages']['total_services']+1;
			$this->Packages->save($package);
		
		
		$this->layout=false;
		$this->render(false);
		return true;
	}
	public function delete_total($id){
		$package = $this->Packages->read(null,$id); 
		//var_dump($package);
		if( $package['Packages']['total_services'] > 0)
		{
			$package['Packages']['total_services'] = $package['Packages']['total_services']-1;
			$this->Packages->save($package);
		}
		
		$this->layout=false;
		$this->render(false);
		return true;
	}


	public function listing(){
		$pagination_settings= array();

		$pagination_settings['limit'] =30;
		$this->Paginator->settings =$pagination_settings;
    $packages_clients = $this->Paginator->paginate('PackagesClients');
		$this->set('data',$packages_clients);
		$this->render('packages_listings_sold');
	}
	public function buy(){


		if($this->data){
			//var_dump($this->data);
			$this->PackagesClients->set($this->data);
			if($this->PackagesClients->save()){
				$this->redirect('/packages/listing');
				//$this->Session->setFlash(__("Error", true), 'flash', array('class' => 'alert alert-error'));	
			}	


		}

		$packages = $this->Packages->find('list');
		$packages_codes = $this->Packages->find('list',array('fields' => array('Packages.id', 'Packages.code')));	

		$clients_codes = $this->Clients->find('list',array('fields' => array('Clients.id', 'Clients.dni')));		
		$clients = $this->Clients->find('list');	

		$this->set('clients_codes',$clients_codes);	
		$this->set('clients',$clients);	
		$this->set('packages',$packages);
		$this->set('packages_codes',$packages_codes);
		//$this->render(false);
	}

	public function buy_edit($id){
		if(!empty($this->data))
		{
			$this->PackagesClients->id = $id;  
			$this->PackagesClients->set($this->data);			
			if (!empty($this->data)){
	    	$this->PackagesClients->id = $id;
	      if ($this->PackagesClients->save($this->data)) {
	        $this->redirect('/packages/listing');
	      }
	    }
			else{
				$errors = $this->Packages->validationErrors;
				$this->Session->setFlash(__("Verifique los valores ingresados, algun dato es incorrecto", true), 'flash', array('class' => 'alert alert-error'));
			}	
		}
		else{
			$this->request->data=$this->PackagesClients->findById($id);
		
		}
		$packages = $this->Packages->find('list');
		$packages_codes = $this->Packages->find('list',array('fields' => array('Packages.id', 'Packages.code')));	

		$clients_codes = $this->Clients->find('list',array('fields' => array('Clients.id', 'Clients.dni')));		
		$clients = $this->Clients->find('list');	

		$this->set('clients_codes',$clients_codes);	
		$this->set('clients',$clients);	
		$this->set('packages',$packages);
		$this->set('packages_codes',$packages_codes);
		$this->render("buy");
	}

	public function detail($id)
	{
		$package_client=$this->PackagesClients->findById($id);
		$package_items=$this->PackagesItems->find('all',array('conditions'=>array('PackagesItems.packages_id'=>$package_client['Packages']['id'])));
		//var_dump($package_client);
		//var_dump($package_items);
		$this->set('package_client',$package_client);
		$this->set('package_items',$package_items);
	}

}
